﻿#region COPYRIGHT© 2009-2013 Phillip Clark. All rights reserved.

// For licensing information see License.txt (MIT style licensing).

#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace FlitBit.Json
{
	public sealed class JsonMemberObject : JsonMember
	{
		public JsonMemberObject(string name, JsonObject value)
			: base(name, value)
		{}

		public override int Count { get { return (Value as JsonObject).Count; } }

		public override bool IsReadOnly { get { return (Value as JsonObject).IsReadOnly; } }

		public override void Add(JsonValue item)
		{
			throw new NotImplementedException();
		}

		public override void Clear()
		{
			var arr = base.Value as JsonObject;
			arr.Clear();
		}

		public override bool Contains(JsonValue item)
		{
			return (Value as JsonObject).Select(m => m.Value == item)
																	.Count() > 0;
		}

		public override void CopyTo(JsonValue[] array, int arrayIndex)
		{
			var members = new List<JsonValue>((Value as JsonObject).Select(m => m.Value));
			members.CopyTo(array, arrayIndex);
		}

		public override IEnumerator<JsonValue> GetEnumerator()
		{
			var obj = base.Value as JsonObject;
			foreach (var member in obj)
			{
				yield return member.Value;
			}
		}

		public override bool Remove(JsonValue item)
		{
			var obj = base.Value as JsonObject;
			var members = obj.Where(m => m.Value == item);
			var removed = false;
			foreach (var member in members)
			{
				removed = obj.Remove(member) || removed;
			}
			return removed;
		}
	}
}